package org.weishe.tmall;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;

/**
 * 获得喜欢矩阵
 * 
 * @author Andrew
 *
 */

/**
 * 按用户分组，计算所有物品出现的组合列表，得到用户对物品的推荐得分矩阵 输入文件格式 i114,u2730,click,2014/9/15 16:41
 * i115,u2726,click,2014/9/4 20:13 i115,u2734,click,2014/9/16 19:36
 * i115,u2979,click,2014/9/1 12:45
 * 
 * 输出文件格式 u2730 i115:2 i114:3
 * 
 */
public class Step2 {
	private static Map<String, Integer> R = new HashMap<String, Integer>();

	static {
		R.put("click", 1);
		R.put("collect", 2);
		R.put("cart", 3);
		R.put("alipay", 4);
	}

	public static void run() {

	}

	public static class Step2Mapper extends Mapper<LongWritable, Text, Text, Text> {
		private Text outputKey = new Text();
		private Text outputValue = new Text();

		@Override
		protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)
				throws IOException, InterruptedException {
			// 获取用户每次操作item只有产生的喜爱度
			String line[] = value.toString().split(",");
			int v = R.get(line[3]);
			outputKey.set(line[1]);
			outputValue.set(line[0] + ":" + v);
			context.write(outputKey, outputValue);
			// u2730 i115:2
			// u2730 i115:3
		}
	}

	public static class Step2Reducer extends Reducer<Text, Text, Text, Text> {
		@Override
		protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context)
				throws IOException, InterruptedException {
			// 合并用户对同一个商品喜爱度，按照同一个用户为键去输出
			Map<String, Integer> m = new HashMap<String, Integer>();
			for (Text t : values) {
				String v[] = t.toString().split(":");
				Integer iv = m.get(v[0]);
				int sumv = 0;
				if (iv != null) {
					
				}

			}
		}

	}
}
